package com.example.javamaday.leetcode;

public class LeetCodeTest0917 {

    public static void main(String[] args) {

    }

    public String reverseOnlyLetters(String s) {
        if (s.length() <= 1) {
            return s;
        }

        char[] chars = s.toCharArray();

        int left = 0;
        int right = chars.length - 1;
        while (left < right) {
            if (isNotLetter(chars[left]) && isNotLetter(chars[right])) {
                left++;
                right--;
                continue;
            }
            if (isNotLetter(chars[left]) && !isNotLetter(chars[right])) {
                left++;
                continue;
            }

            if (!isNotLetter(chars[left]) && isNotLetter(chars[right])) {
                right--;
                continue;
            }

            char temp = chars[left];
            chars[left] = chars[right];
            chars[right] = temp;
            left++;
            right--;
        }

        return new String(chars);
    }

    private boolean isNotLetter(char c) {
        if (c < 65 || (c > 90 && c < 97) || c > 1220) {
            return true;
        } else {
            return false;
        }
    }
}
